package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuShowDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class MenuShowServiceImpl implements MenuShowService {
    @Autowired
    MenuShowDao menuShowDao;
    @Override
    public List<Map<String, Object>> findMenu(String username) {
        List<Menu> list = menuShowDao.findMenuListByUsername(username);

        List<Map<String, Object>> result=new ArrayList<>();
        List<Menu> menuline=new ArrayList<>();
        List<Menu> menuMain=new ArrayList<>();

        for (Menu menu2 : list) {
            if (menu2.getIcon()==null){
                menuline.add(menu2);
            }
        }
        for (Menu menu3 : list) {
            if (menu3.getIcon()!=null){
                menuMain.add(menu3);
            }
        }
        for (Menu menu : menuMain) {
            Map<String,Object> map=new HashMap<>();
            if (menu.getIcon()!=null){
                map.put("path",menu.getPath());
                map.put("title",menu.getName());
                map.put("linkUrl",menu.getLinkUrl());
                List<Map<String,Object>> children=new ArrayList<>();
                for (Menu menu1 : menuline) {
                    Map<String,Object> childrenMap=new HashMap<>();
                    log.debug(menu1.getPath().substring(1,2));
                    if (menu.getPath().equals(menu1.getPath().substring(1,2))){
                        childrenMap.put("path",menu1.getPath());
                        childrenMap.put("title",menu1.getName());
                        childrenMap.put("linkUrl",menu1.getLinkUrl());
                        childrenMap.put("children","");
                        children.add(childrenMap);
                    }
                }
                map.put("children",children);
            }

                result.add(map);

        }
        return result;
    }
}
